﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Z1029.NoteEditor
{
    public class HitEffectManager : MonoBehaviour
    {
        public GameObject perfectEffect, goodEffect;
        public GameObject[] badEffects;
        //bad有好多

        [Header("For Other Platform")]
        public AudioClip[] hitSounds;
        public AudioSource hitEffectSound;

        private void Awake()
        {
            float num = LevelController.noteSize * 10 / 2.5f;
            perfectEffect.transform.localScale = new Vector3(num, num, num);
            //goodEffect.transform.localScale = new Vector3(num, num, num);
            //float num1 = LevelController.noteSize;
            //for (int i = 0; i < badEffects.Length; i++)
            //{
            //    badEffects[i].transform.localScale = new Vector3(num1, num1, num1);
            //}
            InitHitSounds();
        }

        public void InitHitSounds()
        {
            hitEffectSound.volume = LevelController.hitSoundsVolume;
        }

        public void InitANASounds()
        {
        }

        /// <summary>
        /// soundID写0没有声音，写1播放实际0的音效
        /// </summary>
        public void ShowEffect(Note.HitACC acc, Vector3 pos, Transform parent, int soundID, NoteType type)
        {
            if (soundID != 0 && acc != Note.HitACC.Bad) PlayHitSoundById(soundID);
            GameObject effect = null;
            switch (acc)
            {
                case Note.HitACC.Perfect:
                    effect = Instantiate(perfectEffect, pos, new Quaternion());
                    effect.transform.parent = parent;
                    effect.transform.localPosition = new Vector3(effect.transform.localPosition.x, 0, 0);
                    effect.transform.parent = null;
                    break;
                case Note.HitACC.Good:
                    effect = Instantiate(goodEffect, pos, new Quaternion());
                    effect.transform.parent = parent;
                    effect.transform.localPosition = new Vector3(effect.transform.localPosition.x, 0, 0);
                    effect.transform.parent = null;
                    break;
                case Note.HitACC.Bad:
                    effect = Instantiate(badEffects[(int)type], pos, new Quaternion());
                    effect.transform.parent = parent;
                    effect.transform.localPosition = new Vector3(effect.transform.localPosition.x, effect.transform.localPosition.y, 0);
                    effect.transform.localEulerAngles = Vector3.zero;
                    break;
            }
            if (effect != null) Destroy(effect, 0.3f);
        }

        /// <summary>
        /// 通过ANA播放音效
        /// </summary>
        public void PlayHitSoundById(int soundID)
        {
            hitEffectSound.PlayOneShot(hitSounds[soundID - 1]);
        }
    }
}
